package net.m510.autosurfer;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.logging.Logger;

import net.m510.autosurfer.aeoncard.AeonCardBillDatail;
import net.m510.autosurfer.aeoncard.AeonCardCreditDatail;
import net.m510.autosurfer.aeoncard.AeonCardCsvDownload;
import net.m510.autosurfer.aeoncard.AeonCardGoPdfDownload;
import net.m510.autosurfer.aeoncard.AeonCardGoTop;
import net.m510.autosurfer.aeoncard.AeonCardLogin;
import net.m510.autosurfer.aeoncard.AeonCardLoginChecker;
import net.m510.autosurfer.aeoncard.AeonCardLogout;
import net.m510.autosurfer.aeoncard.AeonCardPdfDownload;
import net.m510.autosurfer.common.DocumentGetProcess;
import net.m510.autosurfer.common.cli.OutDirOption;
import net.m510.autosurfer.common.cli.PasswordOption;
import net.m510.autosurfer.core.AbstractMain;
import net.m510.autosurfer.core.exception.UserProcessException;
import net.m510.autosurfer.core.utils.AnalyzeUtils;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.HttpClientBuilder;
import org.jsoup.nodes.Document;

public class AeonCardGetMain extends AbstractMain {
	private static final Logger log = Logger.getLogger(AeonCardGetMain.class.getName());
	
	private String memberId;
	private String password;
	private File outDir;

	/**
	 * 
	 */
	public AeonCardGetMain() {
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		AeonCardGetMain main = new AeonCardGetMain();
		main.setLogger(log);
		main.Main(args);
	}
	@Override
	protected void customClientBuilder(HttpClientBuilder builder) {
		RequestConfig config = RequestConfig.custom().setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY).build();
		builder.setDefaultRequestConfig(config);
	}

	@Override
	protected Document start() throws IOException, UserProcessException {
		AeonCardGoTop p = new AeonCardGoTop(client,context);
		Document out = p.process(null);
		return out;
	}

	@Override
	protected Document login(Document doc) throws IOException,UserProcessException {
		AeonCardLogin p = new AeonCardLogin(client,context);
		p.setMemberId(memberId);
		p.setPassword(password);
		Document out = p.process(doc);
		new AeonCardLoginChecker().check(out);
		log.fine(out.html());
		return out;
	}

	@Override
	protected Document prepareDownload(Document doc) throws IOException,UserProcessException {
		// iframe内のソースを取得し読み捨てる
		log.info("MyPage内のcreditcard　iframe取得開始");
		DocumentGetProcess p = new DocumentGetProcess(client,context) {
			@Override
			protected URI getUri(Document doc) {
				return AnalyzeUtils.resolveURIByPath(context, "/NetBranch/mypage_creAndPoint.do");
			}
		};
		Document out0 = p.process(doc);
		log.finer(out0.html());
		log.info("MyPage内のcreditcard　iframe取得終了");
		// 支払い分詳細確認へ
		log.info("支払い分詳細確認への遷移開始");
		Document out1 = new AeonCardCreditDatail(client, context).process(doc);
		log.info("支払い分詳細確認への遷移終了");
		
		// ご利用明細照会へ
		log.info("ご利用明細照会への遷移開始");
		Document out2 = new AeonCardBillDatail(client, context).process(out1);
		log.info("ご利用明細照会への遷移終了");
		
		// ブランチ：CSVダウンロード
		log.info("CSVダウンロード開始");
		File csvfile = new AeonCardCsvDownload(client, context, outDir).process(out2);
		log.info("CSVダウンロード終了 filename="+csvfile);
		
		// メイン：ご利用明細ダウンロードへ遷移
		log.info("ご利用明細ダウンロードへ遷移開始");
		Document out3 = new AeonCardGoPdfDownload(client, context).process(out2);
//		log.fine(out3.html());
		log.info("ご利用明細ダウンロードへ遷移終了");
		
		return out3;
		
	}

	@Override
	protected File download(Document doc) throws IOException, UserProcessException {
		// メイン：PDFパスワードなしダウンロード
		AeonCardPdfDownload p = new AeonCardPdfDownload(client,context,outDir);
		File f = p.process(doc);
		return f;
	}

	@Override
	protected void logout(Document doc) throws IOException, UserProcessException {
		AeonCardLogout p = new AeonCardLogout(client,context);
		p.process(doc);
	}
	
	@SuppressWarnings("static-access")
	@Override
	protected Options getOptions() {
		Options options = new Options();
		Option out = new OutDirOption(false);options.addOption(out);
		options.addOption(OptionBuilder.hasArg(true).withArgName("memberid")
				.isRequired(true).withDescription("ID")
				.create("memberid"));
		Option password = new PasswordOption(true); options.addOption(password);
		return options;
	}

	@Override
	protected void setParams(CommandLine cmd) {
		memberId = cmd.getOptionValue("memberid");
		this.password = cmd.getOptionValue(PasswordOption.OPT);
		outDir = new File(cmd.getOptionValue(OutDirOption.OPT, "."));
	}

}
